const query = require("../db/mysql")
const { error, success } = require("../module/module")

class rentHoust {
    getRent = async (ctx) => {
        let sql = `select * from rentHouse where 1=1 `
        let data = await query(sql)
        ctx.body = {
            code: 1,
            data
        }
    }
    getbuyHoust = async (ctx) => {
        let sql = `select * from buyHoust where 1=1 `
        let data = await query(sql)
        ctx.body = {
            code: 1,
            data
        }
    }
    selectData = async (ctx) => {
        let sql = `select * from FloorHeight where 1=1 `
        let data = await query(sql)
        ctx.body = success(data)
    }
    //查找详情
    findRent = async (ctx) => {
        let { id, state } = ctx.request.body
        if (state === '求租') {
            let sql = `select * from rentHouse where 1=1 and id=${id}`
            let data = await query(sql)
            if (data.length) {
                ctx.body = success(data)
            }

        } else {
            let sql = `select * from buyHoust where 1=1 and id=${id}`
            let data = await query(sql)
            if (data.length) {
                ctx.body = success(data)
            }
        }
    }

    //筛选求租数据
    selectHoust = async (ctx) => {
        let { roomType, area } = ctx.request.body
        if (area) {
            let sql = `select * from rentHouse where area like "%${area}%" `
            let data = await query(sql)
            if (data.length) {
                ctx.body = success(data, '匹配房源')
            } else {
                ctx.body = error('没有符合条件的房源')
            }
        }
        if (roomType) {
            switch (roomType) {
                case "Multi-storey residential": {//多层住宅
                    let sql = `select * from rentHouse where roomType like "%${'多层住宅'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "High-rise residential": {//高层住宅
                    let sql = `select * from rentHouse where roomType like "${'高层住宅'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "Small high-rise": {//小高层
                    let sql = `select * from rentHouse where roomType like "${'小高层'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "shops": {//商铺/门面房
                    let sql = `select * from rentHouse where roomType like "${'商铺/门面房'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "workshop": {//厂房
                    let sql = `select * from rentHouse where roomType like "${'厂房'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "Office buildings": {//写字楼
                    let sql = `select * from rentHouse where roomType like "${'写字楼'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "villa": {//独院/别墅
                    let sql = `select * from rentHouse where roomType like "${'独院/别墅'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "garage": {//车库
                    let sql = `select * from rentHouse where roomType like "${'车库'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                default: ""
            }
        }
    }
    //筛选求购数据
    selectBuy = async (ctx) => {
        let { roomType, area } = ctx.request.body
        if (area) {
            let sql = `select * from buyHoust where area like "%${area}%" `
            let data = await query(sql)
            if (data.length) {
                ctx.body = success(data, '匹配房源')
            } else {
                ctx.body = error('没有符合条件的房源')
            }
        }
        if (roomType) {
            switch (roomType) {
                case "Multi-storey residential": {//多层住宅
                    let sql = `select * from buyHoust where roomType like "%${'多层住宅'}%" `
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data, '匹配房源')
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "High-rise residential": {//高层住宅
                    let sql = `select * from buyHoust where roomType like "${'高层住宅'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "Small high-rise": {//小高层
                    let sql = `select * from buyHoust where roomType like "${'小高层'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "shops": {//商铺/门面房
                    let sql = `select * from buyHoust where roomType like "${'商铺/门面房'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "workshop": {//厂房
                    let sql = `select * from buyHoust where roomType like "${'厂房'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "Office buildings": {//写字楼
                    let sql = `select * from buyHoust where roomType like "${'写字楼'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "villa": {//独院/别墅
                    let sql = `select * from buyHoust where roomType like "${'独院/别墅'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                case "garage": {//车库
                    let sql = `select * from buyHoust where roomType like "${'车库'}"`
                    let data = await query(sql)
                    if (data.length) {
                        ctx.body = success(data)
                    } else {
                        ctx.body = error('没有符合条件的房源')
                    }
                    break;
                }
                default: ""
            }
        }
    }

}


module.exports = new rentHoust()